#!/bin/sh
#
# Copyright (C) 2017 Dream Property GmbH
#

source librecovery

usage()
{
	echo "Usage: ${0} [-bhqrtgv] <dreambox-image.tar.xz>"
	exit ${1}
}

BACKUP_SETTINGS=":"
RESTORE_SETTINGS=":"
GPT_RECREATE=false
PASSWORT_REMOVE=false
SETTINGS="settings.tar.gz"
UPDATE_CACHE_SIGNED=":"

while getopts bchqrtgpv opt; do
	case "${opt}" in
		b)
			BACKUP_SETTINGS="backup-settings ${SETTINGS}"
			;;
		c)
			UPDATE_CACHE_SIGNED="update_cache_signed"
			;;
		r)
			RESTORE_SETTINGS="restore-settings ${SETTINGS}"
			;;
		g)
			GPT_RECREATE=true
			;;
		p)
			PASSWORT_REMOVE=true
			;;
	esac
	std_opt "${opt}"
done

shift $((${OPTIND} - 1))
[ "$#" -eq 1 ] || usage 1
FILENAME="${1}"

assert_rescue_mode

DEVICE=${FLASH_DEVICE:-${ROOT_PARTITION}}
is_writable_blockdev "${DEVICE}" || is_writable_chardev "${DEVICE}" || abort "Target device '${DEVICE}' is not writable"
! is_empty "${FILENAME}" || abort "No tarball filename given"

case "${FILENAME}" in
	*://*)
		BASE_URI="$(dirname "${FILENAME}")"
		FILENAME="$(basename "${FILENAME}")"
		;;
	*)
		BASE_URI=""
		FILENAME=$(xrealpath "${FILENAME}")
		is_readable_file "${FILENAME}" || abort "Cannot access '${FILENAME}'"
		;;
esac

create_workspace

mount_cache

if [ -n "${BASE_URI}" ]; then
	create_keyring_for_uri "${BASE_URI}/${FILENAME}"
	fetch_signed "${BASE_URI}" "${FILENAME}"
fi

[ ! -f "${RECOVERY_CACHE}/settings.tar.gz" ] || cp ${RECOVERY_CACHE}/settings.tar.gz ${SETTINGS}
if blkdev_has_filesystem "${ROOT_PARTITION}" || is_chardev "${ROOT_PARTITION}"; then
	${BACKUP_SETTINGS}
fi
[ "${RESTORE_SETTINGS}" = ":" -o -f "${SETTINGS}" ] || abort "Can't restore settings without prior backup!"

unmount "${ROOT_MOUNTPOINT}" || warn "Failed to unmount root filesystem!"

if $GPT_RECREATE || ! is_blockdev "${ROOT_PARTITION}" || ! blkdev_has_filesystem "${DATA_PARTITION}"; then
	if is_beneath_directory "${FILENAME}" "${DATA_MOUNTPOINT}"; then
		xcp "${FILENAME}" .
		FILENAME=`basename "${FILENAME}"`
	fi
	unmount "${DATA_MOUNTPOINT}" || abort "Failed to unmount data filesystem"
	if $GPT_RECREATE || ! is_blockdev "${ROOT_PARTITION}" || ! is_blockdev "${DATA_PARTITION}"; then
		create_partition_table "${FLASH_DEVICE}" $GPT_RECREATE || abort "Failed to partition disk"
	fi
	if ! blkdev_has_filesystem "${DATA_PARTITION}"; then
		create_filesystem "${DATA_NAME}" "${DATA_PARTITION}" || warn "Failed to create ${FILESYSTEM} filesystem '${DATA_NAME}' on '${DATA_PARTITION}'"
	fi
	mount_cache
fi
create_filesystem "${ROOT_NAME}" "${ROOT_PARTITION}" || abort "Failed to create ${FILESYSTEM} filesystem '${ROOT_NAME}' on '${ROOT_PARTITION}'"


safe_mount "${ROOT_PARTITION}" "${ROOT_MOUNTPOINT}" || abort "Failed to mount root filesystem"
extract_tarball "${FILENAME}" "${ROOT_MOUNTPOINT}" || abort "Failed to extract root filesystem"
run_postinsts "${ROOT_MOUNTPOINT}" ${POSTINSTS} || abort "Failed to run postinst scripts"
${RESTORE_SETTINGS}
if $PASSWORT_REMOVE; then
	root_password_remove
fi
if is_legacy_version && [ -d "${ROOT_MOUNTPOINT}/etc" ]; then
	touch "${ROOT_MOUNTPOINT}/etc/.rescue-update-suggested"
fi
if $GPT_RECREATE; then
	create_bootmenu
fi
unmount "${ROOT_MOUNTPOINT}" || warn "Failed to unmount root filesystem"

[ "${BACKUP_SETTINGS}" = ":" ] || update_cache "${SETTINGS}"
[ -z "${BASE_URI}" ] || ${UPDATE_CACHE_SIGNED} "${FILENAME}"
unmount_cache

info "Finished successfully. You may reboot now."
